<template>
  <el-dialog :visible.sync="dialogVisible" width="540px" title="新建跟进" center
             :before-close="close"
             :close-on-click-modal="false" :close-on-press-escape='false'>
    <el-form ref="form" label-width="83px" :model="form" :rules="rules" @keyup.enter.native='submit'>
      <el-form-item label="跟进方式:">
        <el-select v-model="form.way" clearable size="small" style="width: 219px" ref="modeSelect">
          <el-option
            v-for="item,index in options"
            :key="index"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="跟进内容:" prop="followMessage">
        <el-input v-model="form.followMessage" type="textarea" rows="5"
                  :show-word-limit="true" maxlength="200"
                  size="small" placeholder="跟进内容"></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer" align="center">
      <el-button @click="close" size="mini">取 消</el-button>
      <el-button type="primary" size="mini" @click="submit" :disabled="loading">{{loading?'保存中':'保 存'}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {flowUp} from '@/api/customer'

    export default {
        name: "DialogFollow",
        data() {
            return {
                dialogVisible: false,
                form: {
                    custid: '',
                    id: '',
                    way: '',
                    followMessage: ''
                },
                rules: {
                    followMessage: [
                        {required: true, message: '填写跟进内容', trigger: 'blur'},
                    ],
                },
                options:[
                    {
                        value: '5',
                        label: '快速记录'
                    },
                    {
                        value: '6',
                        label: '电话'
                    },
                    {
                        value: '7',
                        label: '会面'
                    },
                    {
                        value: '8',
                        label: '社交'
                    }
                ],
                loading:false
            }
        },
        methods: {
            init(customer) {
                this.form.id = customer.id;
                this.form.custid = customer.custid;
                this.dialogVisible = true;
            },
            submit() {//内容  客户id 跟进类型  联系人id
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.loading=true;
                        flowUp(this.form.followMessage, this.form.custid, this.form.way, this.form.id).then(res => {
                            if (res.data.success) {
                                this.$message({
                                    type: 'success',
                                    message: res.data.msg
                                })
                                this.close();
                            }
                        }).catch(()=>this.loading=false)
                    } else {
                        return false;
                    }
                });
            },
            close() {
                this.loading=false;
                this.dialogVisible = false;
                this.$refs['form'].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>
